package net.gyapp.markim.module.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;
import com.easyder.wrapper.utils.LogUtils;

import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.BaseWebActivity;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.contact.ContactAddActivity;
import net.gyapp.markim.module.contact.GroupAddActivity;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/5.
 * Desc:
 */
public class QrScanActivity extends WrapperSwipeActivity<CommonPresenter> implements QRCodeView.Delegate {

    @BindView(R.id.zxv_qr)
    ZXingView zxvQr;

    public static Intent getIntent(Context context) {
        return new Intent(context, QrScanActivity.class);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_qr_scan;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("扫一扫");
        getRootView().setDividerDrawable(null);
        zxvQr.setDelegate(this);
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    @Override
    protected void onStart() {
        super.onStart();
        zxvQr.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
        zxvQr.startSpotAndShowRect(); // 显示扫描框，并且延迟0.5秒后开始识别
    }

    @Override
    protected void onStop() {
        zxvQr.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zxvQr.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        zxvQr.startSpot(); // 延迟0.5秒后开始识别
        onScanSuccess(result);
    }


    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    private void setFlashlight(boolean isOpen) {
        if (isOpen) {
            zxvQr.openFlashlight();
        } else {
            zxvQr.closeFlashlight();
        }
    }

    private void decodeQrFromFile(String path) {
        zxvQr.decodeQRCode(path);

//        String s = QRCodeDecoder.syncDecodeQRCode(path);
    }

    private void onScanSuccess(String result) {
        LogUtils.info("--> " + result);
        if (!TextUtils.isEmpty(result)) {
            if (result.startsWith("http")) {
                startActivity(BaseWebActivity.getIntent(mActivity, result));
            } else {
                if (result.startsWith("user:")) {
                    startActivity(ContactAddActivity.getIntent(mActivity, result.replace("user:", "")));
                } else if (result.startsWith("group:")) {
                    startActivity(GroupAddActivity.getIntent(mActivity, result.replace("group:", "")));
                } else {
                    startActivity(ScanResultActivity.getIntent(mActivity, result));
                }

            }
            finish();
        }
    }
}
